//
//  UserModal.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/4/20.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "UserModal.h"
#import "API.h"

@implementation UserModal

-(instancetype)initWithDic:(NSDictionary *)dic;

{
    if (self = [super init])
    {
        self.userCode = dic[@"userCode"];
        self.account = dic[@"account"];
        self.mobile = dic[@"mobile"];
        NSInteger Validate = [dic[@"mobileValidate"]integerValue];
        if (Validate == 0)
        {
            self.mobileType = MobileNuValidata;
        }else if (Validate == 1)
        {
            self.mobileType = MobileValidata;
        }
        NSInteger userType = [dic[@"userType"]integerValue];
        if (userType == 1)
        {
            self.userType = UserTypeCustomer;
        }else if (userType == 2)
        {
            self.userType = UserTypeMiddleMan;
        }else if (userType == 3)
        {
            self.userType = UserTypeTrader;
        }
        self.status = [dic[@"status"]integerValue];
        self.userId = [dic[@"id"]integerValue];
        
        self.advertisesArr = dic[@"advertises"];
        
    }
    return self;
}
+(instancetype)userModelWithDic:(NSDictionary *)dic;
{
    return [[self alloc]initWithDic:dic];
}


+ (id)allocWithZone:(struct _NSZone *)zone
{
    static UserModal *instance;
    
    // dispatch_once是线程安全的，onceToken默认为0
    static dispatch_once_t onceToken;
    // dispatch_once宏可以保证块代码中的指令只被执行一次
    dispatch_once(&onceToken, ^{
        // 在多线程环境下，永远只会被执行一次，instance只会被实例化一次
        instance = [super allocWithZone:zone];
    });
    
    return instance;
}

/**
 *  将某个对象写入文件时会调用
 *  在这个方法中说清楚哪些属性需要存储
 */
- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.editLockSiteArr forKey:@"editLockSiteArr"];
    [encoder encodeObject:self.editName forKey:@"editName"];
    [encoder encodeObject:self.prmisedNames forKey:@"prmisedNames"];
    [encoder encodeObject:self.premiseIds forKey:@"premiseIds"];
    [encoder encodeInteger:self.userId forKey:@"userId"];
    [encoder encodeInteger:self.status forKey:@"status"];
    [encoder encodeInteger:self.userType forKey:@"userType"];
    [encoder encodeInteger:self.mobileType forKey:@"mobileType"];
    [encoder encodeObject:self.mobile forKey:@"mobile"];
    [encoder encodeObject:self.account forKey:@"account"];
    [encoder encodeObject:self.userCode forKey:@"userCode"];
    [encoder encodeFloat:self.keyboardHeight forKey:@"keyboardHeight"];
    [encoder encodeObject:self.premise forKey:@"premise"];
    [encoder encodeObject:self.img forKey:@"img"];
    [encoder encodeObject:self.advertisesArr forKey:@"advertisesArr"];
    

}

/**
 *  从文件中解析对象时会调用
 *  在这个方法中说清楚哪些属性需要存储
 */
- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        // 读取文件的内容
        self.userCode = [decoder decodeObjectForKey:@"userCode"];
        self.account = [decoder decodeObjectForKey:@"account"];
        self.mobile = [decoder decodeObjectForKey:@"mobile"];
        
        NSInteger Validate = [decoder decodeIntegerForKey:@"mobileType"];
        if (Validate == 0)
        {
            self.mobileType = MobileNuValidata;
        }else if (Validate == 1)
        {
            self.mobileType = MobileValidata;
        }
        NSInteger userType = [decoder decodeIntegerForKey:@"userType"];
        if (userType == 1)
        {
            self.userType = UserTypeCustomer;
        }else if (userType == 2)
        {
            self.userType = UserTypeMiddleMan;
        }else if (userType == 3)
        {
            self.userType = UserTypeTrader;
        }

        self.status = [decoder decodeIntegerForKey:@"status"];
        self.userId = [decoder decodeIntegerForKey:@"userId"];
        self.premiseIds = [decoder decodeObjectForKey:@"premiseIds"];
        self.prmisedNames = [decoder decodeObjectForKey:@"prmisedNames"];
        self.editName = [decoder decodeObjectForKey:@"editName"];
        self.editLockSiteArr = [decoder decodeObjectForKey:@"editLockSiteArr"];
        self.keyboardHeight = [decoder decodeFloatForKey:@"keyboardHeight"];
        self.premise = [decoder decodeObjectForKey:@"premise"];
        self.img = [decoder decodeObjectForKey:@"img"];
        self.advertisesArr = [decoder decodeObjectForKey:@"advertisesArr"];
    }
    return self;
}

+(NSInteger)getCustomerId;
{
    return [UserModal shareUserModel].userId;
}
+(NSString *)getuserCode;
{
    return [UserModal shareUserModel].userCode;
}

+(NSInteger)getUserID;
{
    return [UserModal shareUserModel].userId;
}

+(NSString*)getDetailId;
{
    return @"9";
}
+ (instancetype)shareUserModel
{
    
    return [[self alloc] init];
}

+ (instancetype)shareUserModel2
{
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2.获得文件的全路径
    NSString *path = [doc stringByAppendingPathComponent:@"usermodel"];
    
    UserModal *mod =  [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    return mod;
    
    
}

@end
